const http = require('http')
const logger = require('../../utils/log')
const querystring = require('querystring')

const https = require('https')

// createServer 称为高阶函数
const server = http.createServer((request, response) => {
  // IncomingMessage ServerResponse 
  // const url = request.url
  /*
  let data = ''
  request.on('data', (chunk) => {
    data+=chunk
  })

  // application/x-www-form-urlencoded
  request.on('end', () => {
    // logger.debug(request)
    response.writeHead(200, {
      // 'content-type': 'text/plain'
      'content-type': 'application/json;charset=utf-8'
    })
    
    logger.debug(data)
    // response.write('<div>123</div>')
    // response.end('{"x": 100}')
    response.write(JSON.stringify(querystring.parse(data)))
    response.end()
    // debugger;
  })
  */
 https.get('https://www.xiaomiyoupin.com/mtop/market/cat/list', (res) => {
   let data = ''
    res.on('data', (chunk) => {
      data += chunk
    })
    res.on('end', () => {
      response.writeHead(200, {
        'content-type': 'application/json;charset=utf-8'
      })
      response.end(data)
    })
 })
})

server.listen(8000, () => {
  console.log('localhost:8000')
})

//# node的浏览器调试
// node --inspect server.js
// node --inspect --inspect-brk server.js
// 浏览器：chrome://inspect   watch -> resquest